package com.itmk.web.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.course.entity.Course;
import com.itmk.web.course.entity.CourseParm;
import com.itmk.web.course.entity.SelectCourse;
import com.itmk.web.course.service.CourseService;
import com.itmk.web.student_course.entity.StudentCourse;
import com.itmk.web.student_course.service.StudentCourseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author java实战基地
 * @Version 3501754007
 */
@RestController
@RequestMapping("/api/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private StudentCourseService studentCourseService;

    //新增课程
    @PostMapping
    public ResultVo add(@RequestBody Course course) {
        course.setCourseTime(new Date());
        boolean save = courseService.save(course);
        if(save){
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

    //编辑课程
    @PutMapping
    public ResultVo edit(@RequestBody Course course) {
        course.setCourseTime(new Date());
        boolean save = courseService.updateById(course);
        if(save){
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

    //删除
    @DeleteMapping("/{courseId}")
    public ResultVo delete(@PathVariable("courseId") Long courseId){
        boolean save = courseService.removeById(courseId);
        if(save){
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }


    @GetMapping("/list")
    public ResultVo getList(CourseParm courseParm){
        //构造分页对象
        IPage<Course> page = new Page<>();
        page.setCurrent(courseParm.getCurrentPage());
        page.setSize(courseParm.getPageSize());
        //构造查询条件
        QueryWrapper<Course> query = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(courseParm.getCourseName())){
            query.lambda().like(Course::getCourseName,courseParm.getCourseName());
        }
        if(StringUtils.isNotEmpty(courseParm.getCourseType())){
            query.lambda().eq(Course::getCourseType,courseParm.getCourseType());
        }
        //查询
        IPage<Course> list = courseService.page(page, query);
        return ResultUtils.success("查询成功",list);
    }

    @GetMapping("/getSelectCourse")
    public ResultVo getSelectCourse(CourseParm courseParm){
        //查询
        IPage<Course> list = courseService.getSelectCourse(courseParm);
        return ResultUtils.success("查询成功",list);
    }

    //学生选课
    @PostMapping("/selectCourse")
    public ResultVo selectCourse(@RequestBody StudentCourse studentCourse){
        //查询是否选课
        QueryWrapper<StudentCourse> query = new QueryWrapper<>();
        query.lambda().eq(StudentCourse::getCourseId,studentCourse.getCourseId())
                .eq(StudentCourse::getStuId,studentCourse.getStuId())
        .eq(StudentCourse::getTeacherId,studentCourse.getTeacherId());
        StudentCourse one = studentCourseService.getOne(query);
        if(one != null){
            return ResultUtils.error("您已经选择课程，不用重复选择!");
        }
        boolean save = studentCourseService.save(studentCourse);
        if(save){
            return ResultUtils.success("选择成功!");
        }
        return ResultUtils.error("选择失败!");
    }

    //学生退课
    @PostMapping("/returnCourse")
    public ResultVo returnCourse(@RequestBody StudentCourse studentCourse){
        //条件
        QueryWrapper<StudentCourse> query = new QueryWrapper<>();
        query.lambda().eq(StudentCourse::getCourseId,studentCourse.getCourseId())
                .eq(StudentCourse::getStuId,studentCourse.getStuId())
        .eq(StudentCourse::getTeacherId,studentCourse.getTeacherId());
        boolean remove = studentCourseService.remove(query);
        if(remove){
            return ResultUtils.success("退课成功!");
        }
        return ResultUtils.error("退课失败!");
    }

    //学生已选课程列表
    @GetMapping("/getStuCourse")
    public ResultVo getStuCourse(CourseParm courseParm){
        //查询
        IPage<SelectCourse> list = courseService.getStuCourse(courseParm);
        return ResultUtils.success("查询成功",list);
    }

    //查询教师课程列表
    @GetMapping("/getTeacherCourse")
    public ResultVo getTeacherCourse(CourseParm courseParm){
        //查询
        IPage<Course> list = courseService.getTeacherCourse(courseParm);
        return ResultUtils.success("查询成功",list);
    }
}
